package com.cbank.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public class DateTimeUtil {

	private static Calendar calendar = Calendar.getInstance();
	private static SimpleDateFormat dateFormat = new SimpleDateFormat(DateTimeUtil.DEFAULT_FORMAT_DATE);
	private static byte[] lock = new byte[0];
	public static final String DEFAULT_FORMAT_DATE = "yyyy-MM-dd";
	public static final String DEFAULT_FORMAT_TIME = "HH:mm:ss";
	public static final String DEFAULT_FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
	/** The int EXPIRE_SECOND */
	public static final int EXPIRE_SECOND = 1;
	/** The int EXPIRE_MINUTE */
	public static final int EXPIRE_MINUTE = EXPIRE_SECOND * 60;
	/** The int EXPIER_HOUR */
	public static final int EXPIER_HOUR = EXPIRE_MINUTE * 60;
	/** The int EXPIRE_DAY */
	public static final int EXPIRE_DAY = EXPIER_HOUR * 24;
	/** The int EXPIRE_WEEK */
	public static final int EXPIRE_WEEK = EXPIRE_DAY * 7;
	/** The int EXPIRE_YEAR */
	public static final int EXPIRE_YEAR = EXPIRE_DAY * 365;
	/** The int EXPIRE_DEFAULT */
	public static final int EXPIRE_DEFAULT = EXPIER_HOUR * 3;

	/**
	 * 格式化时间
	 * 
	 * @param time
	 * @return
	 */
	public static String formatTime(Date time, String pattern) {
		if (time == null) {
			return StringUtils.EMPTY;
		}
		synchronized (lock) {
			if (pattern != null && !DateTimeUtil.DEFAULT_FORMAT_DATE.equals(pattern)) {
				return new SimpleDateFormat(pattern).format(time);
			}
			return dateFormat.format(time);
		}
	}

	/**
	 * 格式化时间
	 * 
	 * @param time
	 * @return
	 */
	public static String formatTime(Date time) {
		if (time == null) {
			return StringUtils.EMPTY;
		}
		synchronized (lock) {
			return dateFormat.format(time);
		}
	}

	/**
	 * 解析时间
	 * 
	 * @param time
	 * @return
	 */
	public static Date parseTime(String dateTimeStr, String pattern) {

		if (StringUtils.isEmpty(dateTimeStr)) {
			return null;
		}

		try {
			DateFormat dateFormat = new SimpleDateFormat(pattern);
			return dateFormat.parse(dateTimeStr);
		} catch (Exception e) {
			return null;
		}

	}

	/**
	 * 添加指定的小时，返回添加好的日期 如果为负则是减去
	 * 
	 * @param start
	 * @param i
	 * @return
	 */
	public static Date addSecond(Date startDate, int second) {
		synchronized (lock) {
			calendar.setTime(startDate);
			calendar.add(Calendar.SECOND, second);
			return calendar.getTime();
		}
	}

	/**
	 * 添加指定的天数，返回添加好的日期 如果为负则是减去
	 * 
	 * @param start
	 * @param i
	 * @return
	 */
	public static Date addDay(Date startDate, int day) {
		synchronized (lock) {
			calendar.setTime(startDate);
			calendar.add(Calendar.DAY_OF_YEAR, day);
			return calendar.getTime();
		}
	}
	
	/**
	 * 得到一天的最后一刻
	 * 
	 * @param day
	 * @return
	 */
	public static Date getStartTimeOfDay(Date day) {
		synchronized (lock) {
			calendar.setTime(day);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 000);
			return calendar.getTime();
		}
	}

	/**
	 * 得到一天的最后一刻
	 * 
	 * @param day
	 * @return
	 */
	public static Date getEndTimeOfDay(Date day) {
		synchronized (lock) {
			calendar.setTime(day);
			calendar.set(Calendar.HOUR_OF_DAY, 23);
			calendar.set(Calendar.MINUTE, 59);
			calendar.set(Calendar.SECOND, 59);
			calendar.set(Calendar.MILLISECOND, 999);
			return calendar.getTime();
		}
	}
	
	/**
	 * 获取当前日期的小时
	 * @param day
	 * @return
	 */
	public static int getHour(Date date) {
		synchronized (lock) {
			calendar.setTime(date);
			return calendar.get(Calendar.HOUR_OF_DAY);
		}
	}
	
	/**
	 * 获取当前的月
	 * @param day
	 * @return
	 */
	public static int getMonth(Date date) {
		synchronized (lock) {
			calendar.setTime(date);
			return calendar.get(Calendar.MONTH);
		}
	}
	/**
	 * 获取当前的月的第几天
	 * @param day
	 * @return
	 */
	public static int getDayOfMonth(Date date) {
		synchronized (lock) {
			calendar.setTime(date);
			return calendar.get(Calendar.DAY_OF_MONTH);
		}
	}
	/**
	 *	获取当前时间的 周几
	 * @param day
	 * @return
	 */
	public static int getDay(Date date) {
		synchronized (lock) {
			calendar.setTime(date);
			return calendar.get(Calendar.DAY_OF_WEEK);
		}
	}

	/**
	 * 根据出生日期计算年龄
	 * 
	 * @param birthDay
	 * @return 未来日期返回0
	 * @throws Exception
	 */
	public static int getAge(Date birthDay){
		if (birthDay == null) {
			return 0;
		}
		Calendar cal = Calendar.getInstance();

		if (cal.before(birthDay)) {
			return 0;
		}

		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH);
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(birthDay);

		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;

		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else {
				age--;
			}
		}
		return age;
	}

	public static void main(String[] args) {
		System.out.println(DateTimeUtil.getAge(DateTimeUtil.parseTime("2015-01-01", "yyyy-MM-dd")));
	}
}
